-- death_spoil
-- Created by zhuangsl Sep/13/2016
-- 死亡时给予玩家奖励

return {
    apply = function(prop, baseValue)
        -- 提升的值
        return prop[3];
    end,

    -- 怪物死亡后触发
    trigger = function(who, prop)
        -- 不是怪物
        if who.type ~= OBJECT_TYPE_MONSTER then
            return;
        end

        -- 如果该怪物已经被盗取过了
        if who.dbase:query("be_stolen") == 1 then
            return;
        end

        local package = MonsterM.query(who.classId, "package") or {};
        local normal = package["normal"];
        local better = package["better"];

        -- 没有东西可以盗取
        if not normal and not better then
            return;
        end

        -- 概率偷不到物品
        if DungeonM.getRandSeed("stealing_suc") % 1000 >= prop[3] then
            return;
        end

        local item = normal;
        local rand = DungeonM.getRandSeed("stealing") % 1000;
        if better and rand < 250 then
            -- 高级的
            item = better;
        end

        -- 如果有多个，就随机一个
        if type(item[1]) == "table" then
            rand = DungeonM.getRandSeed("stealing") % #item;
            table.sort(item, function(a, b) return a[2] * 1000 + a[3] < b[2] * 1000 + b[3] end);
            item = item[rand + 1];
        end

        -- 标记为被盗过了
        who.dbase:set("be_stolen", 1);

        -- 不再掉落(除了宝箱怪)
        local grid = DungeonM.getGridByPos(who:getPos());
        local keep = true;
        if grid.type ~= GRID_TYPE_BOX and grid.type ~= GRID_TYPE_COFFIN and type(package["stolen_still_drop"]) ~= "number" then
            grid.bonus = nil;
            keep = false;
        end

        -- 奖励就行了
        BonusM.doBonus(item, "death_spoil");

        -- 抛出事件
        -- 抛出通用建筑奖励事件
        EventMgr.fire(event.BE_STOLEN, {
            ["pos"]   = who:getPos(),
            ["type"]  = GRID_TYPE_MONSTER,
            ["bonus"] = { ["bonus"] = item },
            ["remainGrid"] = true,
            ["keep"]  = keep,
            ["noAlert"] = true,
            -- ["cost"] = eventCost,
        });

        -- 抛出事件尝试完成：竞技场获得对手物品
        EventMgr.fire(event.GET_ARENA_ENEMY_ITEM, { ["pos"] = who:getPos(), ["bonus"] = item, });
    end,

    record = function(target, prop, path)
        return false;
    end
};